﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using StudioLib;
using System.IO;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace StuCat
{
    public partial class CreateWindow : Form
    {
        Studio studio;
        List<Rent> rentList = new List<Rent>();
        List<Photo> photos = new List<Photo>();

        public CreateWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openImageDialog.ShowDialog() == DialogResult.OK)
                for (int i = 0; i < openImageDialog.FileNames.Length; i++)
                {
                    string comment = String.Format("NewPhoto #{0}", photolistBox1.Items.Count);
                    photos.Add(new Photo(Image.FromFile(openImageDialog.FileNames[i]), comment));
                    photolistBox1.Items.Add(comment);
                }
        }


        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = photolistBox1.SelectedIndex;
            if (index != -1)
            {
                pictureBox1.Image = photos[index].Data;
                photoComment.Text = photos[index].Comment;

                photoComment.Enabled = true;
                pictureBox1.Enabled = true;
            }
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            int index = photolistBox1.SelectedIndex;
            Photo newphoto = photos[index];
            newphoto.Comment = photoComment.Text;

            photolistBox1.Items[index] = photoComment.Text;

            photos[index] = newphoto;
        }

        private void button2_Click(object sender, EventArgs e)
        {

            Rent rent = new Rent(new Equipment("New item", "New item description", "www.itemURL.dm"), 1000);

            rentList.Add(rent);

            rentlistBox2.Items.Add("New item");
            rentlistBox2.SelectedIndex = rentlistBox2.Items.Count - 1;
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = rentlistBox2.SelectedIndex;
            if (index != -1)
            {
                rentname.Enabled = true;
                rentURL.Enabled = true;
                rentDescript.Enabled = true;

                numericUpDown1.Enabled = true;

                rentname.Text = rentList[index].Item.Name;
                rentURL.Text = rentList[index].Item.URL;
                rentDescript.Text = rentList[index].Item.Description;

                numericUpDown1.Value = rentList[index].Price;
            }
        }


        private void CreateForm_Load(object sender, EventArgs e)
        {
            //  studio = new Studio();
            subwaycomboBox2.SelectedIndex = 0;
            disctrictcomboBox1.SelectedIndex = 0;
            addrcomboBox3.SelectedIndex = 0;

            dataGridView1.Rows.Clear();
        }

        private void DumpData()
        {
            studio = new Studio();
            studio.Name = nametextBox.Text;

            Int32.TryParse(keytextBox.Text, out studio.Key);
            if (!studio.CheckKey())
            {
                MessageBox.Show("Неверный ключ");
                return;
            }
            studio.Discrict = disctrictcomboBox1.Text;
            studio.Subway = subwaycomboBox2.Text;
            studio.Addr.postfix = addrcomboBox3.Text;
            studio.Addr.streetName = addrstreet.Text;
            studio.Addr.buildingNumber = addrbuilding.Text;

            studio.Description = descripttextBox1.Text;
            studio.Phones = phonestextBox1.Text;

            //Schedule and price...
            int rowCount = dataGridView1.Rows.Count;
            PriceSchedule sched = new PriceSchedule();
            sched.Table = new int[rowCount][];

            for (int rowNumerator = 0; rowNumerator < rowCount; rowNumerator++)
            {
                var row = dataGridView1.Rows[rowNumerator];
                PriceSchedule.Timing intervals = new PriceSchedule.Timing();

                Regex regex = new Regex(@"(\d{1,})\.00 - (\d{1,})\.00");
                Match m = regex.Match((string)row.Cells[0].Value.ToString());
                intervals.start = Int32.Parse(m.Result("$1"));
                intervals.end = Int32.Parse(m.Result("$2"));
                sched.Intervals.Add(intervals);

                sched.Table[rowNumerator] = new int[7];
                for (int i = 0; i < 7; i++)
                {
                    try
                    {
                        sched.Table[rowNumerator][i] = (int)row.Cells[i + 1].Value;
                    }
                    catch
                    {
                        sched.Table[rowNumerator][i] = Int32.Parse((string)row.Cells[i + 1].Value);
                    }
                }
            }

            studio.Schedule = sched;

            studio.Rents = rentList;
            studio.Photos = photos;

            studio.Stamp = DateTime.Now;

            foreach (Room item in roomlistBox1.Items)
            {
                studio.Rooms.Add(item);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // Saves created studio to file selected in dialog
            saveStudioDialog.FileName = this.nametextBox.Text;
            if (saveStudioDialog.ShowDialog() != DialogResult.OK)
                return;

            DumpData();

            studio.SaveToFile(saveStudioDialog.FileName);
        }


        private void rentname_TextChanged(object sender, EventArgs e)
        {
            if (rentlistBox2.SelectedIndex == -1)
                return;

            rentList[rentlistBox2.SelectedIndex].Item.Name = rentname.Text;
            rentlistBox2.Items[rentlistBox2.SelectedIndex] = rentname.Text;
        }

        private void rentURL_TextChanged(object sender, EventArgs e)
        {
            if (rentlistBox2.SelectedIndex == -1)
                return;

            rentList[rentlistBox2.SelectedIndex].Item.URL = rentURL.Text;
        }

        private void rentDescript_TextChanged(object sender, EventArgs e)
        {
            if (rentlistBox2.SelectedIndex == -1)
                return;

            rentList[rentlistBox2.SelectedIndex].Item.Description = rentDescript.Text;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (rentlistBox2.SelectedIndex == -1)
                return;

            rentList[rentlistBox2.SelectedIndex].Price = (int)numericUpDown1.Value;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            CreateRoomBox createRoom = new CreateRoomBox();

            if (createRoom.ShowDialog() != DialogResult.OK)
                return;

            roomlistBox1.Items.Add(createRoom.Room);
        }

        public void LoadStudioToGui(Studio studio)
        {
            disctrictcomboBox1.Text = studio.Discrict;
            subwaycomboBox2.Text = studio.Subway;
            addrcomboBox3.Text = studio.Addr.postfix;
            addrstreet.Text = studio.Addr.streetName;
            addrbuilding.Text = studio.Addr.buildingNumber;
            nametextBox.Text = studio.Name;
            descripttextBox1.Text = studio.Description;
            phonestextBox1.Text = studio.Phones;

            rentList = studio.Rents;

            rentlistBox2.Items.Clear();
            foreach (Rent item in studio.Rents)
                rentlistBox2.Items.Add(item.Item.Name);

            photos = studio.Photos;

            photolistBox1.Items.Clear();
            foreach (Photo item in studio.Photos)
                photolistBox1.Items.Add(item.Comment);

            roomlistBox1.Items.Clear();
            foreach (Room item in studio.Rooms)
            {
                roomlistBox1.Items.Add(item);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (openStudioDialog.ShowDialog() != DialogResult.OK)
                return;

            studio = Studio.FromFile(openStudioDialog.FileName);

            LoadStudioToGui(studio);
        }



        private void button5_Click_1(object sender, EventArgs e)
        {
            ScheduleItemAddBox box = new ScheduleItemAddBox();
            if (box.ShowDialog() != DialogResult.OK)
                return;

            int id = dataGridView1.Rows.Add();

            dataGridView1.Rows[id].Cells[0].Value = box.timing;
            for (int i = 1; i < 8; i++)
            {
                dataGridView1.Rows[id].Cells[i].Value = box.price;
            }
        }

        private void photolistBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                int index = this.photolistBox1.SelectedIndex;
                if (index == -1)
                    return;

                photos.RemoveAt(index);
                photolistBox1.Items.RemoveAt(index);
            }
        }

        private void rentlistBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                int index = rentlistBox2.SelectedIndex;
                if (index == -1)
                    return;

                rentList.RemoveAt(index);
                rentlistBox2.Items.RemoveAt(index);
            }
        }

        private void roomlistBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                int index = roomlistBox1.SelectedIndex;
                if (index == -1)
                    return;

                roomlistBox1.Items.RemoveAt(index);
            }
        }

        private void roomlistBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int id = roomlistBox1.SelectedIndex;
            if (id == -1)
                return;

            Room room = (Room)roomlistBox1.Items[id];

            CreateRoomBox editBox = new CreateRoomBox();
            editBox.Room = room;

            if (editBox.ShowDialog() == DialogResult.OK)
                roomlistBox1.Items[id] = editBox.Room;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Saves created studio to file selected in dialog

            DumpData();


            ServiceRef.StudioSrvClient client = new StuCat.ServiceRef.StudioSrvClient();

            client.UploadStudio(studio);


            //studio.SaveToFile(saveStudioDialog.FileName);
        }
    }




}

